import React, { Component } from "react";
// ant
import { Modal, message } from "antd";
// API
import { getAccountDetail, updateAccount } from "@/api/account";
// 组件
import FormCom from "@c/form/Index";
// 加密
import { enDES3_pass } from "@/utils/validate";

class ChangePasswordModal extends Component {
    constructor(props){
        super(props);
        this.state = {
            isModalVisible: false,
            account_id: "",
            formConfig: {
                editKey: "",
                initValue: {
                    number: 0,
                    status: "",
                    parentId: ""
                },
                setFieldValue: {},
                formatFormKey: "id"
            },
            formLayout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 }
            },
            formItem: [
                { 
                    type: "Input",
                    label: "账号名称", 
                    name: "name", 
                    required: true, 
                    // style: { width: "200px" },
                    placeholder: "请输入账号名称"
                },
                { 
                    type: "Input",
                    label: "用户名/ak", 
                    name: "access_key_id", 
                    required: true,
                    // style: { width: "200px" },
                    placeholder: "请输入AccessKeyID",
                },
                { 
                    type: "Input",
                    label: "新密码/sk", 
                    name: "access_key_secret", 
                    required: true,
                    // style: { width: "200px" },
                    placeholder: "新密码/sk",
                }
            ]
        };
    }

    componentDidMount(){
        this.props.onRef(this);
    }

    onFormRef = (ref) => {
        this.child2 = ref;
    }

    // 关闭弹窗
    handleCancel = () => {
        // 清单表单
        this.child2.onReset();
        this.setState({
            isModalVisible: false,  // 关闭弹窗
            formConfig: { setFieldValue: "" },  // 清除form的初始数据
        })
    }
    
    /** 弹窗 */
    visibleModal = (params) => {
        this.setState({
            isModalVisible: params.status,
            account_id: params.account_id
        }, () => {
            this.getDetailed()
        })
    }

    /** account详情 */
    getDetailed = () => {
        if(!this.state.account_id) { return false; }
        getAccountDetail({id: this.state.account_id}).then(response => {
            const data = response.data.data.items[0];
            console.log(data)
            this.setState({
                formConfig: {
                    setFieldValue: data
                }
            })
        })
    }

    onBlurEvent = (e) => {
        const value = e.currentTarget.value;
        if(value) {
            this.updateItem(value ? false : true);
            return false;
        }
    }
    // 提交按钮
    submit = (value) => {
        this.handlerFormEdit(value);
        
    }

    // 编辑 提交表单
    handlerFormEdit= (value) => {
        const requestData = value;
        requestData.access_key_secret = enDES3_pass(value.access_key_secret);
        console.log(requestData)
        const id = this.state.account_id;
        updateAccount(id, requestData).then(response => {
            const responseData = response.data;
            // 提示
            message.info(responseData.msg);
            // 关闭弹准备
            this.handleCancel(false);
        })
    }

    render(){
        const { formItem, formLayout, formConfig } = this.state;
        return (
            <Modal title="修改云平台账号密码" visible={this.state.isModalVisible} footer={null} onCancel={this.handleCancel}>
                <FormCom 
                    onRef={this.onFormRef} 
                    onBlur={this.onBlurEvent} 
                    formItem={formItem} 
                    formLayout={formLayout} 
                    formConfig={formConfig} 
                    submit={this.submit}
                >
                </FormCom>
            </Modal>
        )
    }
}

export default ChangePasswordModal;
